#ifndef ASSEMBLY_H
#define ASSEMBLY_H

/***************************************************************************
				Assembly.h
                             -------------------
Copyright (c) 2009-2010 
Author: Kishor D. Bhalerao (kishor8dm@gmail.com)
Contributors: 

This file is associated with the Ph.D. dissertation,"ON METHODS FOR EFFICIENT
AND ACCURATE DESIGN AND SIMULATION OF MULTIBODY SYSTEMS", Bhalerao K.D,
Rensselaer Polytechnic Institute, 2010.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

1.) Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

2.) Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.

3.) The name of the authors, their employers, or sponsoring organizations may
not be used to endorse or promote products derived from this software without
specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
EVENT SHALL THE AUTHORS, THEIR EMPLOYERS, OR SPONSORING ORGANIZATIONS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
***************************************************************************/

#include <cstdlib>
#include "SimTKmolmodel.h"
#include "SimTKsimbody.h"

#include "DcaPreprocessor.h"
#include "DcaDataTypes.h"
#include "MatOps.h"
#include "HandleEqs.h"
#include "CompositeBody.h"
#include "DcaHelper.h"
#include "MotionConstraint.h"
#include "List.h"

using namespace SimTK;

class Assembly{

	public:
		Assembly(const DcaPreprocessor& DcaObj, MultibodySystem& system, 
			 SimbodyMatterSubsystem& matter, State& state, NoseHooverThermostat& Thermo , const Real& ForceThreshold):
		DcaObj_(DcaObj),system_(system),matter_(matter),state_(state),Therm_(Thermo),FreeJoints_(new int[matter.getNumBodies()-1]),
		nBodies_(matter.getNumBodies()),ForceThreshold_(ForceThreshold),releasedJoint_(false)
		{initializeActiveJointList();}
		
		
		~Assembly(){if(FreeJoints_!=NULL)delete [] FreeJoints_;}
		
		// Assemble and disassemble system. 
		// Calculate the angular acceleration of each body 
		// along wtih constraint forces	
		void AssembleSystem(void);
		
		void calcUdot(Vector& Udot);
		void calcQdotdot(Vector& Qdotdot);
		void calcQdotdotFromUdot(Vector& Qdotdot,const Vector& Udot);
		
		// This function projects the generalized momenta onto a smaller
		// space. Calling this function, also changes the U's of the system
		void projectMomenta();						
		
		// Set up active joint list
		// 1 = active
		// 0 = transition from active to locked
		// -1 = locked. Initially all joints are active	
		// 2 = transition from locked to active (Required to address the Energy concern if at all that 
		// is to be implemented at any point)
		void initializeActiveJointList(){for(int i=0;i<nBodies_-1;++i)FreeJoints_[i]=1;}
		
		// These functions do not change anything in the system. The function
		// project momenta must be called to physically lock the joints
				
		inline void lockJointTrans(const int& JointId){FreeJoints_[JointId]=0;}
		inline void unlockJointTrans(const int& JointId){FreeJoints_[JointId]=2;}
		
		inline int getJointStatus(const int& JointId) const {return FreeJoints_[JointId];}

		ConstraintForce getConstraintForce() {return CF_;};
		
		void printFreeJointList(){for(int i=0;i<nBodies_-1;++i)cout<<FreeJoints_[i]<<"\t";}

		void writeFreeJointList(std::ostream& OutputStream);
		
		inline bool& releasedJointduringAssembly(){return releasedJoint_;}
			
		State& getState(){return state_;}
		MultibodySystem& getSystem(){return system_;}
		SimbodyMatterSubsystem& getMatter(){return matter_;}
		
	protected:
		
		// This function is called by projectMomenta() to update the value
		// of U's in the system
		void updateState();
				
		inline void lockJoint(const int& JointId){FreeJoints_[JointId]=-1;}	
		inline void unlockJoint(const int& JointId){FreeJoints_[JointId]=1;}		
	private:
		MultibodySystem& system_;
		SimbodyMatterSubsystem& matter_;
		State& state_;
		NoseHooverThermostat& Therm_;

		const DcaPreprocessor& DcaObj_; // this is the object contains all the DCA coeffs of each body
		ConstraintForce CF_; 
		AngularAcceleration Ang_; 
		DeltaV Dv_;
		
		// This was added for the linear acceleration of the first body.
		// This is a temperory fix.
		Vec3 LinAcc_;
		
		// List of free joints. 
		// 1 = free, 
		// 0 = Transition from free to locked
		// -1 = locked
		// 2 = transition from locked to free
		int *FreeJoints_;
		
		// Number of bodies in the system
		int nBodies_;
		
		// This is the constraint torque threshold
		Real ForceThreshold_;
		
		bool releasedJoint_;
};

#endif
